package com.example.scribe.backend.handlers;

import com.example.scribe.SyntaxNode;
import com.example.scribe.backend.BytecodeOps;
import com.example.scribe.backend.ExpressionNodeHandler;
import com.example.scribe.backend.LocalVariableAllocator;
import net.bytebuddy.implementation.bytecode.StackManipulation;

/**
 * Handler for number literal expressions.
 */
public final class NumberExpressionHandler implements ExpressionNodeHandler {
    @Override
    public boolean canHandle(String nodeType) {
        return "Terminal:NUMBER".equals(nodeType);
    }

    @Override
    public StackManipulation handle(SyntaxNode node, String generatedClassName, LocalVariableAllocator locals) {
        if (node == null || node.text() == null) {
            return StackManipulation.Trivial.INSTANCE;
        }
        
        try {
            int value = Integer.parseInt(node.text());
            return BytecodeOps.pushInt(value);
        } catch (NumberFormatException e) {
            return StackManipulation.Trivial.INSTANCE;
        }
    }
}

